/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pojos.Employees;
import pojos.Projects;
import pojos.Reports;
import pojos.TimeLog;

/**
 *
 * @author A7med Kotb
 */
@Stateless
public class ReportsFacade extends AbstractFacade<Reports> {

    @PersistenceContext(unitName = "AquantimaISPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ReportsFacade() {
        super(Reports.class);
    }

    /**
     * generate Employee Report
     * @param employee
     * @param startDate
     * @param endDate
     * @return 
     */
    public List getEmployeeReport(Employees employee, Date startDate, Date endDate) {
        System.out.println("###################Start Date: " + startDate);
        System.out.println("###################End Date: " + endDate);
        int diff = (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
        System.out.println("#####################Difference Between The two Dates: " + diff);

        List<EmployeeReport> empreplist = new ArrayList<EmployeeReport>();
        EmployeeReport emprep1 = new EmployeeReport();
        emprep1.setDay("Day 1");
        emprep1.setDate(startDate);
        Query query1 = getEntityManager().createNamedQuery("Reports.findByEmployeeandDate");
        query1.setParameter("empID", employee);
        query1.setParameter("startDate", startDate);
        List<TimeLog> list = query1.getResultList();
        emprep1.setTimelogs(list);
        int hours = 0;
        for (int cc = 0; cc < list.size(); cc++) {
            hours += list.get(cc).getWorkedHours();
        }
        emprep1.setHours(hours);
        empreplist.add(emprep1);
        for (int i = 1; i <= diff; i++) {
            int count = i + 1;
            EmployeeReport emprep = new EmployeeReport();
            emprep.setDay("Day " + count);

            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            cal.add(Calendar.DAY_OF_YEAR, 1);
            startDate = cal.getTime();
            emprep.setDate(startDate);
            Query query = getEntityManager().createNamedQuery("Reports.findByEmployeeandDate");
            query.setParameter("empID", employee);
            query.setParameter("startDate", startDate);
            List<TimeLog> myList = query.getResultList();
            emprep.setTimelogs(myList);
            int hours2 = 0;
            for (int c = 0; c < myList.size(); c++) {
                hours2 += myList.get(c).getWorkedHours();
            }
            emprep.setHours(hours2);
            empreplist.add(emprep);
        }
        return empreplist;
    }

    /**
     * Update Time Log Table
     * @param tl 
     */
    public void update(TimeLog tl) {
        Query query = getEntityManager().createNamedQuery("Reports.update");
        query.setParameter("tlId", tl.getTlId());
        query.executeUpdate();
    }

    /**
     * Update Time Log Table
     * @param tl 
     */
    public void delete(TimeLog tl) {
        Query query = getEntityManager().createNamedQuery("Reports.delete");
        query.setParameter("tlId", tl.getTlId());
        query.executeUpdate();
    }

    public List getProjectReport(Projects project, Date startDate, Date endDate) {

        System.out.println("###################Start Date: " + startDate);
        System.out.println("###################End Date: " + endDate);
        int diff = (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
        System.out.println("#####################Difference Between The two Dates: " + diff);
        List<ProjectReport> proreplist = new ArrayList<ProjectReport>();
        ProjectReport prorep1 = new ProjectReport();
        prorep1.setDay("Day 1");
        prorep1.setDate(startDate);
        Query query1 = getEntityManager().createNamedQuery("Reports.findByProjectandDate");
        query1.setParameter("project", project);
        query1.setParameter("startDate", startDate);
        List<TimeLog> list = query1.getResultList();
        prorep1.setTimelogs(list);
        int hours = 0;
        for (int cc = 0; cc < list.size(); cc++) {
            hours += list.get(cc).getWorkedHours();
        }
        prorep1.setHours(hours);
        proreplist.add(prorep1);
        for (int i = 1; i <= diff; i++) {
            int count = i + 1;
            ProjectReport prorep = new ProjectReport();
            prorep.setDay("Day " + count);

            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            cal.add(Calendar.DAY_OF_YEAR, 1);
            startDate = cal.getTime();
            prorep.setDate(startDate);
            Query query = getEntityManager().createNamedQuery("Reports.findByProjectandDate");
            query.setParameter("project", project);
            query.setParameter("startDate", startDate);
            List<TimeLog> myList = query.getResultList();
            prorep.setTimelogs(myList);
            int hours2 = 0;
            for (int c = 0; c < myList.size(); c++) {
                hours2 += myList.get(c).getWorkedHours();
            }
            prorep.setHours(hours2);
            proreplist.add(prorep);
        }
        return proreplist;
    }

    public void updateTimeLog(int tlId) {
        Query query = getEntityManager().createNamedQuery("Reports.update");
        query.setParameter("tlId", tlId);
        query.executeUpdate();
    }

    public void deleteTL(int tlId) {
        Query query = getEntityManager().createNamedQuery("Reports.delete");
        query.setParameter("tlId", tlId);
        query.executeUpdate();
    }
    
    public Reports getReports(int tlId){
        Query query = getEntityManager().createNamedQuery("Reports.findByTlId");
        query.setParameter("tlId",tlId);
        TimeLog tl=(TimeLog)query.getSingleResult();
        Reports rep=new Reports();
        rep.setTlId(tl.getTlId());
        rep.setTask(tl.getTask());
        rep.setActivity(tl.getActivity());
        rep.setDate(tl.getDate());
        rep.setProject(tl.getProject());
        rep.setCreatedBy(tl.getCreatedBy());
        rep.setStatus(tl.getStatus());
        rep.setWorkedHours(tl.getWorkedHours());
        rep.setCreatedAt(tl.getCreatedAt());
        return rep;
    }
}
